package com.lytfly.flowable.utils.juel;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import org.springframework.stereotype.Component;

import java.io.Serializable;
import java.util.*;

/**
 * @Description: 个性化处理表达式
 * @Author: Bruce.liu
 * @Since:16:54 2021/03/24
 */
@Component(value = "fuel")
public class FlowJuel implements Serializable {

    private static final long serialVersionUID = 1653010136813862205L;

    /**
     * 通过传入的参数获取其中的一个人
     *
     * @param approver 审批人
     * @return
     */
    public String one(String approver) {
        String one = "";
        if (StrUtil.isNotBlank(approver)) {
            String[] userCodeArray = approver.split(",");
            one = userCodeArray[0];
        }
        return one;
    }

    /**
     * 通过传入的数据解析有多少个人员
     * 使用方式${flowJuel.multi(form.startName,form.mgegr,form.test)}
     *
     * @param approver1 传入的数据
     * @return
     */
    public List<String> multi(String approver1, String approver2, String approver3, String approver4, String approver5) {
        Set<String> approverList = new HashSet<>();
        if (StrUtil.isNotBlank(approver1)) {
            this.getApproverCodeList(approverList, approver1);
        }
        if (StrUtil.isNotBlank(approver2)) {
            this.getApproverCodeList(approverList, approver2);
        }
        if (StrUtil.isNotBlank(approver3)) {
            this.getApproverCodeList(approverList, approver3);
        }
        if (StrUtil.isNotBlank(approver4)) {
            this.getApproverCodeList(approverList, approver4);
        }
        if (StrUtil.isNotBlank(approver5)) {
            this.getApproverCodeList(approverList, approver5);
        }
        return new ArrayList<>(approverList);
    }

    private void getApproverCodeList(Set<String> approverList, String approvers) {
        Set<String> codes = new HashSet<>();
        if (StrUtil.isNotBlank(approvers)) {
            String[] userCodes = approvers.split(",");
            Collections.addAll(codes, userCodes);
        }
        if (CollUtil.isNotEmpty(codes)) {
            approverList.addAll(codes);
        }
    }

    public List<String> multi(String approver1, String approver2, String approver3, String approver4) {
        return this.multi(approver1, approver2, approver3, approver4, null);
    }

    public List<String> multi(String approver1, String approver2, String approver3) {
        return this.multi(approver1, approver2, approver3, null, null);
    }

    public List<String> multi(String approver1, String approver2) {
        return this.multi(approver1, approver2, null, null, null);
    }

    public List<String> multi(String approver1) {
        return this.multi(approver1, null, null, null, null);
    }

    /**
     * 判断searchStr是否在str中
     *
     * @param str       整体的字符串
     * @param searchStr 要检索的字符串
     * @return true存在  false不存在
     */
    public boolean exist(String str, String searchStr) {
        boolean flag = false;
        if (StrUtil.isNotBlank(str) && StrUtil.isNotBlank(searchStr)) {
            flag = StrUtil.contains(str, searchStr);
        }
        return flag;
    }

}
